﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyUAMS.BLL;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public partial class Admin_AdminWork : System.Web.UI.Page
{
    AdminServer adminSrv = new AdminServer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            string file = "F:\\web系统应用\\MyUAMS\\MyUAMS2\\txtfile\\type.txt";
            if (!File.Exists(file))
            {

            }
            else
            {
                FileStream fs = null;
                try
                {
                    fs = new FileStream("F:\\web系统应用\\MyUAMS\\MyUAMS2\\txtfile\\type.txt", FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                    Account account = (Account)bf.Deserialize(fs);
                    txtbeizhu.Text = account.beizhu;
                }
                catch (Exception ex)
                {
                    //lblProErr.Text = ex.Message;
                }
                finally
                {
                    fs.Close();
                }
                
            }
           
            
        }
    }
    protected void Bind()
    {
        var products = adminSrv.GetAllAP(0);
        if (products.Count() == 0)
        {
            lblProErr.Text = "没有需要审核的申请。";
        }
        else
        {
            lblProErr.Text = " ";
        }
        dgvAP.DataSource = adminSrv.GetAllAP(0);
        dgvAP.DataBind();
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        int uid;
        int apid = 0;
        int aid = 0;
        string type = "";
        int amount = 0;
        int step = 0;
        int apuid = 0;
        string apuname = "";
        string status = "";
        GridView gvProduct = new GridView();
        gvProduct = (GridView)Page.FindControl("dgvAP");
        if (gvProduct != null)
        {
            for (int i = 0; i < gvProduct.Rows.Count; i++)
            {
                CheckBox chkChoice = new CheckBox();
                chkChoice = (CheckBox)gvProduct.Rows[i].FindControl("chkChoice");
                if (chkChoice != null)
                {
                    if (chkChoice.Checked)
                    {
                        uid = int.Parse(gvProduct.Rows[i].Cells[7].Text);
                        apid = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                        aid = int.Parse(gvProduct.Rows[i].Cells[2].Text);
                        amount = int.Parse(gvProduct.Rows[i].Cells[6].Text);
                        type = gvProduct.Rows[i].Cells[4].Text;
                        step = int.Parse(gvProduct.Rows[i].Cells[9].Text);
                        status = gvProduct.Rows[i].Cells[12].Text;
                        if (Session["AminId"] != null && Session["AdminName"] != null)
                        {
                            apuid = (int)Session["AminId"];
                            apuname = Session["AdminName"].ToString();
                        }
                        else
                        {
                            lblProErr.Text = "会话信息丢失";
                            return;
                        }

                        adminSrv.UpdateAP(apid, aid, type, amount, step, apuid, apuname,status,uid);
                        lblProErr.Text = "审核成功";
                        
                    }
                }
            }
        }
        Bind();

    }

    protected void btnNO_Click(object sender, EventArgs e)
    {
        int uid;
        int apid = 0;
        int aid = 0;
        string type = "";
        int amount = 0;
        int step = 0;
        int apuid = 0;
        string apuname = "";
        string status;
        GridView gvProduct = new GridView();
        gvProduct = (GridView)Page.FindControl("dgvAP");
        if (gvProduct != null)
        {
            for (int i = 0; i < gvProduct.Rows.Count; i++)
            {
                CheckBox chkChoice = new CheckBox();
                chkChoice = (CheckBox)gvProduct.Rows[i].FindControl("chkChoice");
                if (chkChoice != null)
                {
                    if (chkChoice.Checked)
                    {
                        uid = int.Parse(gvProduct.Rows[i].Cells[7].Text);
                        apid = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                        apid = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                        aid = int.Parse(gvProduct.Rows[i].Cells[2].Text);
                        amount = int.Parse(gvProduct.Rows[i].Cells[6].Text);
                        type = gvProduct.Rows[i].Cells[4].Text;
                        step = int.Parse(gvProduct.Rows[i].Cells[9].Text);
                        status = "打回";
                        if (Session["AminId"] != null && Session["AdminName"] != null)
                        {
                            apuid = (int)Session["AminId"];
                            apuname = Session["AdminName"].ToString();
                            adminSrv.UpdateAP(apid, aid, type, amount, step, apuid, apuname, status, uid);
                            lblProErr.Text = "打回成功";
                        }
                        else
                        {
                            lblProErr.Text = "会话信息丢失";
                            return;
                        }

                        
                        

                    }
                }
            }
        }
        Bind();
    }

    protected void btnOne_Click(object sender, EventArgs e)
    {
        var products = adminSrv.GetOneAP(0);
        if (products.Count() == 0)
        {
            lblProErr.Text = "没有需要审核的申请。";
        }
        else
        {
            lblProErr.Text = " ";
        }
        dgvAP.DataSource = adminSrv.GetOneAP(0);
        dgvAP.DataBind();
    }

    protected void btnTwe_Click(object sender, EventArgs e)
    {
        var products = adminSrv.GetTweAP(0);
        if (products.Count() == 0)
        {
            lblProErr.Text = "没有需要审核的申请。";
        }
        else
        {
            lblProErr.Text = " ";
        }
        dgvAP.DataSource = adminSrv.GetTweAP(0);
        dgvAP.DataBind();
    }

    protected void btnThree_Click(object sender, EventArgs e)
    {
        var products = adminSrv.GetThreeAP(0);
        if (products.Count() == 0)
        {
            lblProErr.Text = "没有需要审核的申请。";
        }
        else
        {
            lblProErr.Text = " ";
        }
        dgvAP.DataSource = adminSrv.GetThreeAP(0);
        dgvAP.DataBind();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Bind();
    }

    protected void btnBeiZhu_Click(object sender, EventArgs e)
    {
        FileStream fs = null;
        try
        {
            Account objAccount = new Account()
            {
                beizhu = txtbeizhu.Text.Trim()
            };
            fs = new FileStream("F:\\web系统应用\\MyUAMS\\MyUAMS2\\txtfile\\type.txt", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, objAccount);
        }
        catch (Exception ex)
        {
            //lblProErr.Text = ex.Message;
        }
        finally
        {
            fs.Close();
        }
        
        
    }
    [Serializable]
    class Account
    {
        public string beizhu { get; set; }
    }
}